Skip to content

Don't modify the params hash passed to Client.new - #39

Open
Divyanshu-2907 wants to merge 5 commits into
serpapi:masterfrom
Divyanshu-2907:fix/constructor-params-mutation
Open

Divyanshu-2907 wants to merge 5 commits into
serpapi:masterfrom
Divyanshu-2907:fix/constructor-params-mutation

Conversation

@Divyanshu-2907

Copy link
Copy Markdown

Client#initialize deletes :timeout and :persistent straight from the hash the caller passes in. That causes two problems.

If you build two clients from the same config hash, the second one silently loses those settings and falls back to a 120s timeout and a persistent socket:

config = { engine: 'google', api_key: 'xxx', timeout: 5, persistent: false }
a = SerpApi::Client.new(config)
b = SerpApi::Client.new(config)
b.timeout    # => 120, expected 5
b.persistent # => true, expected false
config       # => { engine: 'google', api_key: 'xxx' }

And a frozen config hash (common for constants) raises:

SerpApi::Client.new({ engine: 'google', timeout: 5 }.freeze)
# => FrozenError: can't modify frozen Hash

The fix is to work on params.dup and take :timeout and :persistent out of the copy. I used dup rather than clone on purpose, because clone keeps the frozen flag and would still raise on a frozen hash. @params is still frozen at the end like before, so nothing changes for callers who pass a fresh hash.

I added spec/serpapi/client/constructor_spec.rb. Those specs only build clients and never hit the API, so they run without a real key.

PR #22 touched this area too, but it was closed along with a lot of other changes. This one is just the constructor fix, so it's easy to review on its own.

The constructor deleted :timeout and :persistent from the caller's hash,
so a reused config hash gave the second client the default 120s timeout
and a persistent socket. A frozen hash raised FrozenError.

Work on a dup of params instead. dup is used rather than clone because
clone keeps the frozen state.
@Divyanshu-2907

Copy link
Copy Markdown
Author

Hi, just a small ping on this when you get time. The CI run needs a maintainer to approve it since it's my first PR here. It only touches the constructor, and the new specs don't need an API key. Happy to change anything you'd like. Thanks!

@jvmvik jvmvik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for highlighting this problem.
I went for a slightly different implementation where the params is not fully duplicated but got the same result.
see: c24449b

The constructor docs say keys can be Symbols or Strings, but timeout,
persistent, api_key, engine, and symbolize_names were only read with
Symbol keys. With String keys, timeout and persistent were ignored and
sent to the API as query parameters, #api_key and #engine returned nil,
and a per-call 'symbolize_names' was ignored and sent to the API.

Read these options through a helper that accepts both key types.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Divyanshu-2907

Copy link
Copy Markdown
Author

Thanks for the review. Using reject instead of dup-then-delete is cleaner, makes sense. Glad the specs were useful.

@jvmvik

jvmvik commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

CI is stuck because credit ran out for live testing. I'm waiting on an admin to recharge. then I'll merge.

@Divyanshu-2907

Copy link
Copy Markdown
Author

Thanks for the update, no rush at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants